package org.tools.wstt.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * Servlet implementation class ConfigServlet
 */
public class ResponseManageServlet extends AHttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		log("req uri : " + pRequest.getRequestURI());
		if (pRequest.getParameter("resp_id") != null) {
			Map vMap = (Map) mContext.getAttribute(APP_RESPONSES_MAPS);
			Map vResponseMap = (Map) vMap.get(pRequest.getParameter("resp_id"));
			if (vResponseMap != null) {
				String vHeadersStr = (String) vResponseMap.get("headers");
				log("Header String : " + vHeadersStr);
				if (!StringUtils.isEmpty(vHeadersStr)
						&& vHeadersStr.indexOf(":") != -1) {
					String[] vHeaders = vHeadersStr.split("\n");
					for (int i = 0; i < vHeaders.length; i++) {
						String[] vCutHeader = vHeaders[i].split(":",2);
						if (vCutHeader.length > 1) {
							log("Header to set : [" + vCutHeader[0] + "]=["
									+ vCutHeader[1]+"]");
							pResponse.setHeader(vCutHeader[0], vCutHeader[1]);
						}
					}
				}
				String vContent = (String) vResponseMap.get("content");
				pRequest.setAttribute("contentRequested", vContent);
			}
		}
		RequestDispatcher vReqDispatcher = pRequest
				.getRequestDispatcher(INTERNAL_PATH_OF_JSPS
						+ "/response_config.jsp");
		vReqDispatcher.forward(pRequest, pResponse);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		log("POST on uri : " + pRequest.getRequestURI());
		Map vMap = (Map) mContext.getAttribute(APP_RESPONSES_MAPS);
		String vRedirectQueryStr = "";
		
		if (pRequest.getParameter("action") != null) {
			if (pRequest.getParameter("action").equals("create")) {
				// resp_id provided when editing existing responses
				String vResponseID = pRequest.getParameter("resp_id");
				
				// or create a new ID based on current time
				if(StringUtils.isEmpty(vResponseID)) {
					vResponseID = ""
						+ new Long(Calendar.getInstance().getTimeInMillis());
				}
				Map vResponseMap = new HashMap();
				vResponseMap.put("headers", pRequest
						.getParameter("httpHeaders"));
				vResponseMap.put("content", pRequest
						.getParameter("httpContent"));
				vResponseMap.put("id", vResponseID);
				log("Save Response ID : " + vResponseID);
				vMap.put(vResponseID, vResponseMap);
			}
			else if (pRequest.getParameter("action").equals("Delete")) {
				vMap.remove(pRequest.getParameter("resp_id"));
			}
			else if (pRequest.getParameter("action").equals("Edit") && !StringUtils.isEmpty(pRequest.getParameter("resp_id"))) {
				vRedirectQueryStr = "?load_resp_id="+pRequest.getParameter("resp_id");
			}
		}
		// RequestDispatcher vReqDispatcher = request
		// .getRequestDispatcher(PATH_OF_JSPS + "config.jsp");
		// vReqDispatcher.forward(request, response);
		// better do a redirect to the page (allow refreshes of the page)
		pResponse.sendRedirect(URI_TOOLS + "/response_config" + vRedirectQueryStr);
		return;
	}

}
